package com.starfleet.galleryofdingao

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.core.content.ContextCompat.getString
import androidx.core.content.ContextCompat.startActivity

class GalleryActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gallery)
        val intent = Intent(this,MainActivity::class.java)
        val web = findViewById<WebView>(R.id.webview)
        web.addJavascriptInterface(WebViewInterface(this,intent), "ActivityJump")
        web.settings.javaScriptEnabled = true
        web.webViewClient = WebViewClient()
        web.setOnKeyListener(View.OnKeyListener { _, keyCode, _ ->
            if (keyCode == KeyEvent.KEYCODE_BACK && web.canGoBack()) {
                web.goBack()
                return@OnKeyListener true
            }
            false
        })
        web.loadUrl("file:///android_asset/contents.html") //call page file url located at "assets"
    }
}

class WebViewInterface(private val mContext : Context, private val intent: Intent) {
    @JavascriptInterface
    fun JumpBack() {
        startActivity(mContext,intent,null)
        Toast.makeText(mContext, getString(mContext,R.string.welcome_back),Toast.LENGTH_LONG).show()
    }
}